package model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import model.loader.XMLInfoLoader;

public class QueryFood {
	
	private static List<Food> foodList = null; 
	
	private static void initListFood(){
		XMLInfoLoader loader = new XMLInfoLoader(); 
		foodList = loader.loadXmlInfo();
	}
	
	public static List<Food> getFoodsByName(String name){
		if(foodList==null){
			initListFood(); 
		}
		List<Food> result = new ArrayList<Food>(); 
		for(Food food: foodList){
			if(food.getName().toUpperCase().indexOf(name.toUpperCase())!= -1 ){
				result.add(food);
			}
		}
		return result; 
	}
	
	public static int sumCalories(List<Food> foodList){
		Iterator<Food> it = foodList.iterator();
		int acum = 0; 
		while(it.hasNext()){
			Food food = (Food) it.next();
			acum += food.getCalories();
		}
		return acum; 
	}
	
}
